#include "StdAfx.h"
#include "CfgFile.h"

CCfgFile::CCfgFile(LPCTSTR lpszFileName)
{
	m_strFileName = lpszFileName;
}

CCfgFile::~CCfgFile()
{

}

int CCfgFile::GetProfileInt(LPCTSTR lpSectionName, LPCTSTR lpKeyName,int nDefault)
{
	int nKeyValue=0;
	TCHAR chBuf[100];
	::GetPrivateProfileString(lpSectionName,lpKeyName,_T(""),chBuf,100,m_strFileName);
	if(chBuf[0]== 0)
		return nDefault;
	nKeyValue=::_ttoi(chBuf);
	return nKeyValue;
}

float CCfgFile::GetProfileFloat(LPCTSTR lpSectionName, LPCTSTR lpKeyName,float fDefault)
{
	float fKeyValue=0.0f;
	TCHAR chBuf[100];
	::GetPrivateProfileString(lpSectionName,lpKeyName,_T(""),chBuf,100,m_strFileName);
	if(chBuf[0]== 0)
		return fDefault;
	fKeyValue=(float)::_tstof(chBuf);
	return fKeyValue;
}

CString CCfgFile::GetProfileStr(LPCTSTR lpSectionName, LPCTSTR lpKeyName,LPCTSTR strDefault)
{
	CString strKeyValue;
	TCHAR chBuf[512];
	::GetPrivateProfileString(lpSectionName,lpKeyName,strDefault,chBuf,512,m_strFileName);

	strKeyValue=chBuf;
	return strKeyValue;
}

BOOL CCfgFile::DeleteSection(LPCTSTR lpSectionName)
{
	return ::WritePrivateProfileString(lpSectionName,NULL,NULL,m_strFileName);
}

BOOL CCfgFile::DeleteKey(LPCTSTR lpSectionName, LPCTSTR lpKeyName)
{
	return ::WritePrivateProfileString(lpSectionName,lpKeyName,NULL,m_strFileName);
}

BOOL CCfgFile::WriteProfileStr(LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString)
{
	return ::WritePrivateProfileString(lpSectionName,lpKeyName,lpString,m_strFileName);
}

BOOL CCfgFile::WriteProfileInt(LPCTSTR lpSectionName, LPCTSTR lpKeyName, int nValue)
{
	TCHAR szValue[20];

	_stprintf(szValue,_T("%d"),nValue);
	return ::WritePrivateProfileString(lpSectionName,lpKeyName,szValue,m_strFileName);
}
